package com.wen.POIProject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * Read Excel File
 * 
 * Notice how each class in POI library starts with HSSF prefix, e.g. HSSFWorkbook, HSSFSheet etc.
 * HSSF stands for Horrible SpreadSheet Format.
 * 
 * Add a comment for SVN checkout, change the version into 1.1				
 * @author wen
 * @version 1.1
 * 
 */
public class App 
{
    public static void main( String[] args )
    {
        try {
			FileInputStream file = new FileInputStream("/Users/wen/Documents/test file/test.xls");
			
			//Get the workbook instance for XLS file
			HSSFWorkbook workbook = new HSSFWorkbook(file);
			
			//Get first sheet from the workbook
			HSSFSheet sheet = workbook.getSheetAt(0);
			
			//Get iterator to all the rows in current sheet
			Iterator<Row> rowIterator = sheet.iterator();
			while(rowIterator.hasNext()){
				Row row = rowIterator.next();
				
				//For each row, iterate through each columns
				Iterator<Cell> cellIterator = row.cellIterator();
				while(cellIterator.hasNext()){
					
					Cell cell = cellIterator.next();
					
					switch(cell.getCellType()){
						case Cell.CELL_TYPE_BOOLEAN:
							System.out.print(cell.getBooleanCellValue() + "\t\t");
							break;
						case Cell.CELL_TYPE_NUMERIC:
							System.out.print(cell.getNumericCellValue() + "\t\t");
							break;
						case Cell.CELL_TYPE_STRING:
							System.out.print(cell.getStringCellValue() + "\t\t");
							break;
					}
				}
				System.out.println("");
			}
			file.close();
			//FileOutputStream out = new FileOutputStream(new File("/Users/wen/Documents/test file/test.xls"));
			//workbook.write(out);
			//out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
